package org.apache.james.mime4j.io;

import java.io.IOException;
import java.io.InputStream;

public class PartialInputStream extends PositionInputStream
{
  private final long limit;

  public PartialInputStream(InputStream paramInputStream, long paramLong1, long paramLong2)
    throws IOException
  {
    super(paramInputStream);
    paramInputStream.skip(paramLong1);
    this.limit = (paramLong1 + paramLong2);
  }

  private int getBytesLeft()
  {
    return (int)Math.min(2147483647L, this.limit - this.position);
  }

  public int available()
    throws IOException
  {
    return Math.min(super.available(), getBytesLeft());
  }

  public int read()
    throws IOException
  {
    int i;
    if (this.limit > this.position)
      i = super.read();
    while (true)
    {
      return i;
      i = -1;
    }
  }

  public int read(byte[] paramArrayOfByte, int paramInt1, int paramInt2)
    throws IOException
  {
    return super.read(paramArrayOfByte, paramInt1, Math.min(paramInt2, getBytesLeft()));
  }

  public long skip(long paramLong)
    throws IOException
  {
    return super.skip(Math.min(paramLong, getBytesLeft()));
  }
}